using Gateway.Common.Models;
using Gateway.Domain.Entities;
using Gateway.IApplication.BaseInput;
using Gateway.IApplication.DataDic;
using Gateway.IApplication.DataDic.Input;
using Gateway.IRepositories;

namespace Gateway.Application;

/// <summary>
/// 数据字典的服务
/// </summary>
public class DataDicApp : IDataDicApp
{
    private readonly IAppRepository<DataDic> _repository;
    private readonly IUnitOfWork _unitOfWork;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="repository"></param>
    /// <param name="unitOfWork"></param>
    public DataDicApp(IAppRepository<DataDic> repository, IUnitOfWork unitOfWork)
    {
        _repository = repository;
        _unitOfWork = unitOfWork;
    }

    /// <summary>
    /// 新增
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
 
    public async Task<string> Add(CreateDataDicInput input)
    {
        var exist = await _repository.FirstOrDefaultAsync(x => x.Code.Equals(input.Code));
        if (exist != null)
            throw new MyException("字典编码已存在");
        var entity = new DataDic(name: input.Name, code: input.Code, value: input.Value, description: input.Description,
            sort: input.Sort, parentId: input.ParentId, dicType: input.DicType);
        await _repository.AddAsync(entity);
        await _unitOfWork.CommitAsync();
        return entity.Id;
    }

    /// <summary>
    /// 编辑
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Update(UpdateDataDicInput input)
    {
        var exist = await _repository.FirstOrDefaultAsync(x => x.Code.Equals(input.Code) && x.Id != input.DataDicId);
        if (exist != null)
            throw new MyException("字典编码已存在");
        var entity = await _repository.GetAsync(input.DataDicId);
        if (entity == null)
        {
            throw new MyException("无效数据");
        }

        entity.Update(name: input.Name, code: input.Code, value: input.Value, description: input.Description,
            sort: input.Sort);
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }

    /// <summary>
    /// 移除
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Remove(RemoveInput input)
    {
        var entity = await _repository.GetAsync(input.Id);
        if (entity == null)
        {
            throw new MyException("无效数据");
        }

        entity.SetDeleted();
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }

    /// <summary>
    /// 禁用/启用
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    /// <exception cref="MyException"></exception>
    public async Task<bool> Disable(DisableInput input)
    {
        var entity = await _repository.GetAsync(input.Id);
        if (entity == null)
        {
            throw new MyException("无效数据");
        }

        entity.SetDisabled(input.Disable);
        await _repository.UpdateAsync(entity);
        await _unitOfWork.CommitAsync();
        return true;
    }
}